import re

from django_redis import get_redis_connection
from rest_framework import serializers

from users.models import User


class CreateUserSerializer(serializers.ModelSerializer):
    '''自定义序列化器类'''
    allow = serializers.CharField(label='是否同意协议', write_only=True)
    password2 = serializers.CharField(label='确认密码', write_only=True)
    sms_code = serializers.CharField(label='短信验证码', write_only=True)
    token = serializers.CharField(label='JWT-token',read_only=True)

    class Meta:
        model = User
        fields = ('id', 'mobile', 'username', 'password', 'password2', 'sms_code', 'allow','token')

        extra_kwargs = {
            'username': {
                'min_length': 5,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许5-20个字符的用户名',
                    'max_length': '仅允许5-20个字符的用户名',
                }
            },
            'password': {
                'write_only': True,
                'min_length': 8,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许8-20个字符的密码',
                    'max_length': '仅允许5-20个字符的密码',
                }
            }

        }

    # 手机号的格式，手机号是否注册过，是否同意协议，短息验证码是否正确,验证俩次密码是否一致
    # 是否同意协议
    def validate_allow(self, value):
        '''是否同意协议'''

        if value != 'true':
        # if value != True:
            raise serializers.ValidationError('请同意协议')
        return value

    def validate_mobile(self, value):
        """验证手机号"""

        if not re.match(r'^1[3-9]\d{9}$', value):
            raise serializers.ValidationError('手机号格式错误')

        # 手机号是否注册
        count = User.objects.filter(mobile=value).count()
        if count > 0:
            raise serializers.ValidationError('该手机号已经注册')

        return value

    def validate(self, attrs):
        """两次密码是否一样,短信验证是否正确"""
        # 获取手机号

        mobile = attrs['mobile']

        # 　验证俩次密码
        password = attrs['password']
        password2 = attrs['password2']
        if password != password2:
            raise serializers.ValidationError('两次密码不一致')

        # 从redis 中获取真实的短信验证码内容，链接redis 数据库
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes

        if not real_sms_code:
            raise serializers.ValidationError('短信验证码已过期')

        # 对比短信验证码
        sms_code = attrs['sms_code']  # str

        # bytes -> str
        real_sms_code = real_sms_code.decode()
        if real_sms_code != sms_code:
            raise serializers.ValidationError('短信验证码错误')
        return attrs

    def create(self, validated_data):
        '''保存注册用户的信息'''
        # 先清楚无用的数据
        del validated_data['password2']
        del validated_data['allow']
        del validated_data['sms_code']
        # 保存注册用户的信息
        # print(validated_data)
        user = User.objects.create_user(**validated_data)


        # 由服务器生成一个jwt token的数据 包含 登录用户的信息,免密码登录
        from rest_framework_jwt.settings import api_settings

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        # 生成 Jwt token
        token = jwt_encode_handler(payload)

        user.token = token
        # 返回注册的用户
        return user